package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenyh
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询分类信息")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询分类信息：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    @ApiOperation(value = "按照type查询分类信息")
    public Result<List<Category>> list(@RequestParam("type") Integer type) {
        log.info("按照type查询分类信息:{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }


    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用或禁用分类")
    public Result startOrStop(@PathVariable("status") Integer status, @RequestParam Long id) {
        log.info("状态:{}, 用户id: {}", status, id);
        categoryService.changeStatus(status, id);
        return Result.success();
    }

    @PostMapping("")
    @ApiOperation(value = "新增分类")
    public Result addNewCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类:{}", categoryDTO);
        categoryService.addNewCategory(categoryDTO);
        return Result.success();
    }

    @PutMapping("")
    @ApiOperation(value = "修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @DeleteMapping("")
    @ApiOperation(value = "删除分类")
    public Result deleteById(@RequestParam("id") Long id) {
        log.info("删除分类:{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

}
